package com.situ.mall.controller;

import com.situ.mall.annotation.Log;
import com.situ.mall.enums.LogOperType;
import com.situ.mall.pojo.Product;
import com.situ.mall.pojo.query.ProductQuery;
import com.situ.mall.pojo.vo.ProductVO;
import com.situ.mall.service.IProductService;
import com.situ.mall.util.LayUITableResult;
import com.situ.mall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    IProductService productService;

    @RequestMapping("/getProductListPage")
    public String getProductListPage() {
        return "product_list";
    }

    @Log(operType = LogOperType.SELECT, isSaveResponseData = false)
    @RequestMapping("/selectByPage")
    @ResponseBody
    public LayUITableResult selectByPage(ProductQuery productQuery) {
        System.out.println("ProductController.selectByPage");
        return productService.selectByPage(productQuery);
    }

    @Log(title = "商品管理", operType = LogOperType.DELETE)
    @RequestMapping("/deleteAll")
    @ResponseBody
    public Result deleteAll(Integer[] ids) {
        boolean isSuccess = productService.deleteAll(ids);
        return isSuccess ? Result.ok("删除成功") : Result.error("删除失败");
    }

    @Log(title = "商品管理", operType = LogOperType.DELETE)
    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        boolean isSuccess = productService.deleteById(id);
        return isSuccess ? Result.ok("删除成功") : Result.error("删除失败");
    }

    @RequestMapping("/getProductInsertPage")
    public String getProductInsertPage() {
        return "product_insert";
    }

    @Log(title = "商品添加", operType = LogOperType.INSERT)
    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(Product product) {
        boolean isSuccess = productService.insert(product);
        return isSuccess ? Result.ok("添加成功") : Result.error("添加失败");
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public Result updateStatus(Product product) {
        boolean isSuccess = productService.updateStatus(product);
        return isSuccess ? Result.ok("更新成功") : Result.error("更新失败");
    }

    @RequestMapping("/getProductUpdatePage")
    public String getProductUpdatePage(Integer id, Model model) {
        ProductVO productVO = productService.selectById(id);
        System.out.println(productVO);
        model.addAttribute("product", productVO);
        return "product_update";
    }

    @Log(title = "商品更新", operType = LogOperType.UPDATE)
    @RequestMapping("/update")
    @ResponseBody
    public Result update(Product product) {
        boolean isSuccess = productService.update(product);
        return isSuccess ? Result.ok("更新成功") : Result.error("更新失败");
    }
}
